/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
var $alert = require('libs/alert');
var $form = require('js-path/formValide.js');
var $form2 = require('js-path/formValideNotSub.js');
var col = 5;
var mms = 3;
$(function(){
    mainFormValidInit();
    resourceDefaultInit();
    $("#main_form input[name=systemId]").on("change",function () {
        var systemId = $(this).val();
        $.ajax({
            url:"api/v1/p/mms/authResource/queryResource",
            type:"post",
            dataType:"json",
            data:{
                systemId:systemId
            },
            success:function (result) {
                if(result.code==200){
                    resourceInit(result.data);
                }else{
                    $alert(result.message);
                }
            }
        })
    })
});
function resourceDefaultInit() {
    $.ajax({
        url:"api/v1/p/mms/authResource/queryResource",
        type:"post",
        data:{
            systemId:mms
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                resourceInit(result.data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function resourceInit(data) {
    $("#div_resource").html("");
    var len = data.length;
    if(len!=0){
        var divArray = new Array(Math.floor((len-1)/col)+1);
        $.each(divArray,function (k,o) {
            divArray[k] = $("<div style='padding-left: 15px'></div>");
        });
        $.each(data,function (k,o) {
            divArray[Math.floor(k/col)].append("<input type='checkbox' name='resourceId' value='"+data[k].id+"'><span style='margin-right: 10px'>"+data[k].name+"</span>");
        });
        $.each(divArray,function (k,o) {
            $("#div_resource").append(divArray[k]);
        });
    }
}
function customValid() {
    var resource = true;
    var len = $("#main_form input[type=checkbox]:checked").length;
    if(len==0){
        $("#div_resource").parent().parent().addClass("has-error");
        $("#div_resource").parent().next("div").html('<p id="authority-error" class="error">资源不能为空</p>');
        resource = false;
    }
    return resource;
}
function mainFormValidInit(){
    $form({selector:'#main_form'},{
        authorityMark:{required:true},authorityName:{required:true},
        issys:{required:true},systemId:{required:true}
    },{authorityMark:{required:'权限标示不能为空'},authorityName:{required:'权限名称不能为空'},
        issys:{required:'是否系统级别不能为空'},systemId:{required:'所属系统不能为空'}
    },function () {
        if(!customValid()){
            return false;
        }
        var json = $('#main_form').serializeObject();
        var resourceIds = json.resourceId;
        var ids = new Array();
        if(typeof resourceIds == "object"){
            $.each(resourceIds,function (k,o) {
                ids.push({"id":o});
            });
        }else{
            ids.push({"id":resourceIds});
        }
        json.ids = ids;
        delete json.resourceId;
        return json;
    },function (result) {
        if(result.code == 200){
            window.location.href="authority-manage";
        }else{
            $alert(result.message);
        }
    })
}


$("input[name='ckeckAll']").click(function(){
    //判断当前点击的复选框处于什么状态$(this).is(":checked") 返回的是布尔类型
    if($(this).is(":checked")){
        $("#div_resource input").prop("checked",true);
    }else{
        $("#div_resource input").prop("checked",false);
    }
});